<?php
class Archive_Idcard_Model extends CI_Model {

	var $table = 'archive_idcard';

	function __construct()
	{
		parent::__construct();
	}
	
	function get_all()
	{
		$query = $this->db->get($this->table);
		//$query = $this->db->query($this->db->_limit('select * from tudi ',1,0));
		return $query->result();
	}

	function get_one($id){
		$query = $this->db->get_where($this->table, array('id' => $id), 1, 0);
		return $query->row();
	}
	
	function insert_entry($data)
	{
		$this->db->insert($this->table, $data);
		return $this->db->insert_id();
	}

	function update_entry($id, $data)
	{
		$this->db->update($this->table, $data, array('id' => $id));
	}
	
	function delete_entry($id){
		$this->db->delete($this->table, array('id' => $id)); 
		//delete relation data
		//...
	}
	
	function update_forms($id, $cateid){
		$info = $this->get_one($id);
		if (empty($info) ) return;
		
		$old = $info->fill_form;
		$arr = explode(',', $old);
		$arr[] = $cateid;
		$arr = array_unique( $arr );
		sort($arr);
		$new = implode(',', $arr);

		if ($old != $new )  {
            $this->update_entry($id, array('fill_form'=>$new));  
        }
        // 更新关联记录的时间; added by zhouhuan, 2012-09-26
        $this->db->query("update visa_user_forms set update_time='".date("Y-m-d H:i:s")."' where card_id=$id");
			
	}
}
